(function() {
    var myApp = angular.module('myApp');

    myApp.run(function($rootScope, reqDataByAjax) {
        // 全局初始化
        reqDataByAjax("/scheduling/api/settings/function/getpermissions").then(function(data) {
            $rootScope.permissionList = data[0];
        });
    });
    //推送菜单数据的controller
    myApp.controller("pushdataController", function($scope, $log, reqDataByAjax) {
        $scope.pushingdata=function(){
            //获得要插入的数据
            reqDataByAjax("/scheduling/api/comm/pushingdata/getmenuData").then(function(menudata) {
                $scope.menuroledata = menudata[0];
                reqDataByAjax("/scheduling/api/comm/pushingdata/pushmenu", $scope.menuroledata, "POST").then(function (data) {
                    alert('推送成功！');
                });
            });
        };

    });
    myApp.controller("myFunctionTreeController", function($scope, $log, reqDataByAjax) {
        // 设置默认系统选项
        $scope.$root.selectedSystemId = 'DM';

        // DM/DP/DA切换监控
        $scope.$root.$watch("selectedSystemId", function(newVal, oldVal, scope) {

            $scope.$root.selectedFunction = null;
            scope.$broadcast('refresh.tree');
        });

        // 设置菜单项树结构的Option
        var url = "/scheduling/api/settings/function/getall";
        function dataSource(parentFunction, callback) {
            reqDataByAjax(url, {
                system_id : $scope.$root.selectedSystemId,
                parent_id : parentFunction.group_id
            }).then(function(data) {
                var ret = [];
                for (var i = 0; i < data[0].length; ++i) {
                    var item = data[0][i];
                    item['type'] = 'folder';
                    ret.push(item);
                }
                callback({ data: ret });
            });
        }
        $scope.option = {
            dataSource: dataSource,
            multiSelect: false,
            folderSelect: true
        };

        // 记录当前选中组
        $scope.$on('selected.fu.tree', function(event, element, e, o) {
            if (element.is('#functiontree')) {
                $scope.$root.selectedFunction = o.selected[0];
                $scope.$root.$broadcast("show.tab", "#groupfntab");
            }
        });
        $scope.$on('deselected.fu.tree', function(event, element) {
            if (element.is('#functiontree')) {
                $scope.$root.selectedFunction = null;
                $scope.$root.$broadcast("show.tab", "#helptab");
            }
        });
    });
    
    myApp.controller("addFunctionController", function($scope, $log
        , reqDataByAjax) {
        // TAB显示前初始化显示内容
        $scope.$on("show.bs.tab", function(event, element, target) {
            if (target.is("#addfunction")) {
                $scope.fn = {
                    authority:'是'
                };
            }
        });

        // 调用服务创建菜单项
        var url = "/scheduling/api/settings/function/addfunction";
        $scope.ok = function() {
            var fn = $scope.fn;
            fn.system_id = $scope.$root.selectedSystemId;
            fn.parent_id = $scope.$root.selectedFunction && $scope.$root.selectedFunction.group_id;
            fn.sort = +fn.sort;
            reqDataByAjax(url, fn, 'POST').then(function() {
                // 刷新树显示
                if (fn.parent_id)
                    $scope.$root.$broadcast("refreshFolder.tree", "#functiontree");
                else
                    $scope.$root.$broadcast("refresh.tree", "#functiontree");
            });
        };
    });

    myApp.controller("modifyFunctionController", function($scope, $log
        , reqDataByAjax) {
        // TAB显示前初始化显示内容
        $scope.$on("show.bs.tab", function(event, element, target) {
            if (target.is("#modifyfunction")) {
                $scope.fn = $scope.$root.selectedFunction;
            }
        });
        // 调用服务修改菜单项
        var url = "/scheduling/api/settings/function/modifyfunction";
        $scope.ok = function() {
            $scope.fn.system_id = $scope.$root.selectedSystemId;
            $scope.fn.sort = +$scope.fn.sort;
            reqDataByAjax(url, $scope.fn, "POST").then(function() {
                $scope.$root.$broadcast("updateItem.tree", "#functiontree", $scope.fn);
            });
        };
    });

    myApp.controller("deleteFunctionController", function($scope, $log
        , reqDataByAjax) {
        // 调用服务删除菜单项
        var url = "/scheduling/api/settings/function/deletefunction";
        $scope.ok = function() {
            var fn = $scope.$root.selectedFunction;
            fn.system_id = $scope.$root.selectedSystemId;
            reqDataByAjax(url, fn, "POST").then(function() {
                if (fn.parent_id)
                    $scope.$root.$broadcast("refreshParentFolder.tree", "#functiontree");
                else
                    $scope.$root.$broadcast("refresh.tree", "#functiontree");
            });
        };
    });


})();

